package com.yc.bran.snack.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.yc.bran.snack.service.IGoodsInfoService;
import com.yc.bran.snack.vo.ResultVo;
import com.yc.bran.snack.bean.GoodsInfo;
import com.yc.bran.snack.enums.ResultEnmu;

@RestController
@RequestMapping("/goods")
public class GoodsInfoController {

	@Autowired
	private IGoodsInfoService goodsInfoService;

	//	@Autowired
	//	private GoodsInfoRepository goodsInfoRepository;

	// 根据tid查商品信息
	@PostMapping("/selectAllGoodsInfo")
	public ResultVo selectAllGoodsInfo(@RequestParam Integer tid) {
		List<GoodsInfo> list = goodsInfoService.selectAllGoodsInfo(tid);
		if(list == null || list.isEmpty()) {
			return new ResultVo(ResultEnmu.DATA_NULL);
		}
		return new ResultVo(ResultEnmu.SUCCESS,list);
	}

	//查看所有商品信息
	@PostMapping("/getgoodsinfo")
	public ResultVo getgoodsinfo() {
		List<GoodsInfo> list = goodsInfoService.getredisGoodsInfo();
		if(list == null || list.isEmpty()) {
			return new ResultVo(ResultEnmu.DATA_NULL);
		}
		//		List<GoodsInfo> list = goodsInfoRepository.findAll();
		System.out.println(list);
		return new ResultVo(ResultEnmu.SUCCESS,list);
	}

	// 根据gid查商品信息
	@PostMapping("/selectGoodsInfo")
	public ResultVo selectGoodsInfo(@RequestParam Integer gid) {
		List<GoodsInfo> list = goodsInfoService.selectGoodsInfo(gid);
		if(list == null || list.isEmpty()) {
			return new ResultVo(ResultEnmu.DATA_NULL);
		}
		return new ResultVo(ResultEnmu.SUCCESS, list);
	}

	// 后台添加商品
	@PostMapping("/add")  
	public ResultVo add(  
			@RequestParam("tid") Integer tid,  
			@RequestParam("gname") String gname,  
			@RequestParam("gprice") Double gprice,  
			@RequestParam("ginfo") String ginfo,  
			@RequestParam("gweight") Double gweight,  
			@RequestParam("gblance") Integer gblance,
			@RequestParam("files[]") List<MultipartFile> files) {  

		// 处理文件上传  
		List<String> urlsString = goodsInfoService.handleFileUpload(files);
		String gpic = String.join(",", urlsString);
		
		System.out.println(gpic + "11111111111111");
		System.out.println(urlsString);

		// 调用服务层添加商品信息  
		int result = goodsInfoService.add(tid, gname, gprice, ginfo, gweight, gblance, gpic);
		

		if (result < 0) {  
			return new ResultVo(ResultEnmu.ERROR);  
		}  
		return new ResultVo(ResultEnmu.SUCCESS);  
	}  

	// 下架商品
	@GetMapping("/close")
	public ResultVo close(@RequestParam Integer gid) {
		int result = goodsInfoService.close(gid);
		if(result < 0) {
			return new ResultVo(ResultEnmu.ERROR);
		}
		return new ResultVo(ResultEnmu.SUCCESS);
	}

	// 上架商品
	@GetMapping("/open")
	public ResultVo open(@RequestParam Integer gid) {
		int result = goodsInfoService.open(gid);
		if(result < 0) {
			return new ResultVo(ResultEnmu.ERROR);
		}
		return new ResultVo(ResultEnmu.SUCCESS);
	}

	//查看所有商品信息
	@PostMapping("/getgoods")
	public ResultVo getgoods() {
		List<GoodsInfo> list = goodsInfoService.getgoods();
		if(list == null || list.isEmpty()) {
			return new ResultVo(ResultEnmu.DATA_NULL);
		}
		return new ResultVo(ResultEnmu.SUCCESS,list);
	}

	@GetMapping("/selectbackgoodsinfo")
	public List<Map<String, Object>> getAllGoodsInfoWithType() {  
		List<Map<String, Object>> goodsInfoList = goodsInfoService.selectbackgoodsinfo(); 
		return goodsInfoList;  
	}  
}
